import jwt
import datetime

import redis


class Myjwt:
    def __init__(self):
        # 初始化秘钥
        self.password = '123'

    # 加密秘钥 设置过期时间
    def encode_jwt_time(self, userinfo, mytime):
        # 单独控制载荷信息
        playlode = {
            # 设置过期时间
            'exp': int((datetime.datetime.now() + datetime.timedelta(seconds=mytime)).timestamp()),
            'data': userinfo
        }
        mystr = jwt.encode(playlode, self.password, algorithm="HS256")
        try:
            mystr = str(mystr, 'utf-8')
        except Exception as e:
            pass
        return mystr

    # 加密
    def encode_jwt(self, userinfo):
        mystr = jwt.encode(userinfo, self.password, algorithm="HS256")
        try:
            mystr = str(mystr, 'utf-8')
        except Exception as e:
            pass
        return mystr

    # 解密
    def decode_jwt(self, mystr):
        return jwt.decode(mystr, self.password, algorthms=['HS256'])

